import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:get/get_rx/src/rx_types/rx_types.dart';
import 'package:photo_view/photo_view_gallery.dart';
import 'package:yinshui_caiying/model/image_model.dart';
import 'package:yinshui_caiying/utils/colors.dart';
import 'package:yinshui_caiying/utils/icons.dart';
import 'package:m_loading/m_loading.dart';

class MyDialog {
  /// 确认弹窗
  /// [title] 是标题
  /// [desc] 是内容
  /// [onConfirm] 是确认按钮的回调方法
  static void confirmDialog({
    String title = '标题',
    String desc = '',
    void Function()? onConfirm,
  }) {
    if (onConfirm == null) {
      onConfirm = () {};
    }
    Widget widget = Column(
      mainAxisAlignment: MainAxisAlignment.center,
      crossAxisAlignment: CrossAxisAlignment.center,
      children: [
        Container(
          width: 888.w,
          // height: 572.h,
          decoration: BoxDecoration(
              color: Colors.white,
              borderRadius: BorderRadius.all(Radius.circular(30.w))),
          child: Column(
            children: [
              Container(
                margin: EdgeInsets.only(top: 61.h, bottom: 78.h),
                child: Text(
                  title,
                  style: TextStyle(
                      fontSize: 56.sp,
                      fontWeight: FontWeight.w600,
                      color: Colors.black),
                ),
              ),
              desc.length > 0
                  ? Container(
                      margin: EdgeInsets.only(bottom: 77.h),
                      child: Text(
                        desc,
                        maxLines: 100,
                        style: TextStyle(
                            fontSize: 36.sp, color: Color.fromRGBO(1, 1, 1, 1)),
                      ),
                    )
                  : Container(),
              Container(
                // height: 100.h,
                margin: EdgeInsets.only(bottom: 64.sp, left: 72.w, right: 72.w),
                decoration: BoxDecoration(
                    border: Border(
                        top: BorderSide(
                            color: Color.fromRGBO(235, 237, 240, 1),
                            width: 1.h))),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    Container(
                      width: 342.w,
                      height: 147.h,
                      child: TextButton(
                        onPressed: () {
                          SmartDialog.dismiss();
                        },
                        child: Text('取消',
                            style: TextStyle(
                                color: Colors.white,
                                fontSize: 40.sp,
                                fontWeight: FontWeight.w500)),
                        style: ButtonStyle(
                            shape: MaterialStateProperty.all(
                                RoundedRectangleBorder(
                              borderRadius: BorderRadius.circular(74.w),
                            )),
                            backgroundColor: MaterialStateProperty.all(
                                Color.fromRGBO(242, 104, 104, 1))),
                      ),
                    ),
                    Expanded(child: SizedBox()),
                    Container(
                      width: 342.w,
                      height: 147.h,
                      child: TextButton(
                        onPressed: onConfirm,
                        child: Text(
                          '确定',
                          style: TextStyle(
                              color: Colors.white,
                              fontSize: 40.sp,
                              fontWeight: FontWeight.w500),
                        ),
                        style: ButtonStyle(
                            shape: MaterialStateProperty.all(
                                RoundedRectangleBorder(
                              borderRadius: BorderRadius.circular(74.w),
                            )),
                            backgroundColor: MaterialStateProperty.all(
                                Color.fromRGBO(11, 193, 173, 1))),
                      ),
                    )
                  ],
                ),
              )
            ],
          ),
        )
      ],
    );
    ;

    SmartDialog.show(
        widget: widget,
        alignmentTemp: Alignment.center,
        clickBgDismissTemp: false);
  }

  /// 打开图片预览弹窗
  ///
  /// [index]初始图片的索引
  ///
  /// [list]图片列表
  static void openImageView(int index, RxList<ImageModel> list) {
    final currentIndex = 0.obs;
    currentIndex.value = index;
    PageController pageController = PageController(initialPage: index);
    SmartDialog.show(
        widget: Stack(
      children: [
        ///图片预览
        Positioned(
            child: PhotoViewGallery.builder(
          scrollPhysics: const BouncingScrollPhysics(),
          onPageChanged: (index) {
            print(index);
            currentIndex.value = index;
          },
          pageController: pageController,
          itemCount: list.length,
          builder: (BuildContext context, int num) {
            return PhotoViewGalleryPageOptions(
              imageProvider: NetworkImage(list[num].path),
            );
          },
        )),
        //关闭按钮
        Positioned(
            top: 80.w,
            right: 30.w,
            child: IconButton(
              padding: EdgeInsets.all(0),
              iconSize: 100.w,
              icon: Icon(
                MyIcons.close,
                color: Colors.white,
                size: 100.w,
              ),
              onPressed: () {
                SmartDialog.dismiss();
              },
            )),
        //图片描述
        Positioned(
            width: 1.sw,
            bottom: 80.w,
            // left: 0.5.sw,
            child: Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                // Text(
                //   list[currentIndex.value].name,
                //   textAlign: TextAlign.center,
                //   style: TextStyle(
                //     color: Colors.white,
                //   ),
                // )
              ],
            ))
      ],
    ));
  }

  ///打开loading
  static showLoading({String? msg, bool isUseExtraWidget = false}) {
    SmartDialog.show(
        widget: Container(
          width: 200.w,
          height: 200.w,
          child: BallCirclePulseLoading(
            ballStyle: BallStyle(
              size: 30.w,
              color: MyColors.main,
              ballType: BallType.solid,
            ),
          ),
        ),
        maskColorTemp: Colors.transparent,
        isLoadingTemp: true,
        isUseExtraWidget: isUseExtraWidget);
  }
}
